#include <stdio.h>
#include <stdbool.h>

size_t MyStrlen(const char*s)
{
    size_t count = 0;
    while ( *s++ != '\0')
    {
        count++;
    }
    return count;

}

void MyStrcpy(char *dest,const char *src)
{
    while(*src != '\0')
    {
        *dest++ = *src++; 
    }
    *dest = '\0';
}

void MyStrncpy(char *dest,const char *src,size_t n)
{
    while(n-- != 0 && *src != '\0')
    {
        *dest++ = *src++;
    }
    *dest = '\0';
}

int MyStrcmp(const char * s1,const char * s2)
{
    while(*s1 != '\0' && *s2 != '\0' && *s1 == *s2 )
    {
        s1++;
        s2++;
    }
    return *s1 - *s2;
}

#define Min(a,b) (a) > (b) ? a : b
int MyStrncmp(const char * s1,const char * s2,size_t n)
{
    // size_t minlen = Min(Min(MyStrlen(s1),MyStrlen(s2)),n);
    // while(*s1 == *s2 && minlen != 1)
    // {
    //     s1++;
    //     s2++;
    // }
    // return *s1 - *s2;
     while(*s1 != '\0' && *s2 != '\0' && *s1 == *s2 && n-- != 1 )
    {
        s1++;
        s2++;
    }
    if(*s1 != '\0' && *s2 != '\0')
        return *s1-- - *s2--;
    else
        return *s1 - *s2;
}

void MyStrcat(char *dest,const char *scr)
{
    while(*dest != '\0')
    {
        *dest++;
    }
    while(*scr != '\0')
    {
        *dest++ = *scr++;
    }
    *dest = '\0';
    //MyStrcpy(dest + MyStrlen(dest),scr);
}

void MyStrncat(char *dest,const char *scr,size_t n)
{
    MyStrncpy(dest + MyStrlen(dest),scr,n);
}

char *  MyStrstr(char *dest,const char *src)
{
    size_t dlen = MyStrlen(dest);
    size_t slen = MyStrlen(src);
    for(int i = 0;i <= dlen - slen;i++)
    {
        if(MyStrncmp(dest + i,src,slen) == 0)
        {
            return dest + i;
        }
    }
    return NULL;
}

bool IsHaveChar(const char *s,char c)
{
    while(*s != '\0')
    {
        if(*s == c)
            return true;
        s++;
    }
    return false;
}

char* MyStrtok(char *src,const char *delim)
{
    //上一次切割完的下一次字符串的
    static char *nextStr = NULL;
    if(src == NULL)//想要继续切割的字符串
        src = nextStr;
    if(src == NULL)//没有字符串可以切割
        return NULL;
    //跳过所有的分割符号
    while(*src != '\0')
    {
        if(IsHaveChar(delim,*src) == true)
        {
            src++;
        }
        else
            break;//找到第一个不是分隔符的
    }
    if(*src == '\0')
        return NULL;

    //src : 有效字符串的首地址
    //找下一个分隔符
    char *end = src;
    while(*end != '\0')
    {   
        //是下一个分隔符
        if(IsHaveChar(delim,*end) == true)
        {
            break;
        }
        else
            end++;
    }
    if(*end == '\0')//没有下一个字符串可以切割了
    {
        nextStr = NULL;
    }
    else
    {
        *end = '\0';
        nextStr = end + 1;
    }
    return src;
}

int main()
{
    // printf("%ld\n",MyStrlen("hello"));
    // const char *s = "llo";
    // char str[20] = "hello,world";
    // // char s1[] = "hello";
    // // char s2[] = "helloworld";
    // char *p = MyStrtok(str,",.");
    // while(p != NULL)
    // {
    //     printf("%s\n",p);
    //     p = MyStrtok(NULL,",.");
    // }
    char str[] ="zhangsan,lisi.wangwu ";
    char *s = MyStrtok(str,",.");
    while(s != NULL)
    {
        printf ("%s\n",s);
        s = MyStrtok(NULL,",.");
    }
    //MyStrcpy(str,s);
    size_t n = 3;
    //MyStrncpy(str,s,n);
    //MyStrcat(str,s);
    // char * p = MyStrstr(str,s);
    // printf("%s\n",p);
    // printf("%s\n",str);
    // printf("%d\n",MyStrncmp(s1,s2,1));
    // printf("%d\n",'w');
    return 0;
} 